package com.cqtczh.qhd.driver.acticity.carteam;

import httptools.HttpJsonTool;

import java.io.Serializable;
import java.lang.reflect.Type;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javabeen.CarTeamIdBeen;

import org.json.JSONObject;

import tools.L;

import View.InPutDialog;
import View.InPutDialog.getUserIntent;
import adapter.CarTeamMemberListAdapter;
import adapter.ProductAdapter;
import android.R.integer;
import android.app.AlertDialog;
import android.app.Dialog;
import android.content.Intent;
import android.graphics.Color;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.View.OnLongClickListener;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.AdapterView.OnItemLongClickListener;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.SimpleAdapter;
import android.widget.TextView;
import android.widget.Toast;

import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import com.lidroid.xutils.HttpUtils;
import com.lidroid.xutils.exception.HttpException;
import com.lidroid.xutils.http.RequestParams;
import com.lidroid.xutils.http.ResponseInfo;
import com.lidroid.xutils.http.callback.RequestCallBack;
import com.lidroid.xutils.http.client.HttpRequest;
import com.lidroid.xutils.http.client.HttpRequest.HttpMethod;
import com.lidroid.xutils.util.PreferencesCookieStore;
import com.cqtczh.qhd.driver.AppContent;
import com.cqtczh.qhd.driver.R;
import com.cqtczh.qhd.driver.acticity.test;
import com.cqtczh.qhd.driver.acticity.base.BaseActivity;
import com.cqtczh.qhd.driver.api.GagApi;
import com.cqtczh.qhd.driver.model.CarTeam;
import com.cqtczh.qhd.driver.model.CarTeamDriver;
import com.cqtczh.qhd.driver.model.OrderTotal;
import com.cqtczh.qhd.driver.vo.JsonRequest;
import com.cqtczh.qhd.driver.vo.JsonResponse;
import com.cqtczh.qhd.driver.vo.State;

public class MyCarTeamMember extends BaseActivity {
	private ListView listView;
	private TextView textView;
	private Intent intent;
	private AppContent app;
	private PreferencesCookieStore preferencesCookieStore;
	private List<OrderTotal> one_driver_order;
	private ImageView head_left, refash;
	private TextView head_title;
	private int LIST_VIEW_NO = 100;
	//
	private int list_pag = 0;
	private int All_member_count = 0;
	private List<CarTeamIdBeen> car_team_driver;
	private String CAR_TEAM_ID;
	private CarTeamMemberListAdapter adapter;
	private List<Map<String, Object>> listdata;
	private Button Adddriver;
	
	private ImageView head_right_img;
	private CarTeamIdBeen Carteamdata;
	private int flag = 0;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_my_car_team_member);
		if (savedInstanceState==null) {
			intent = getIntent();
			if (intent == null) {
				return;
			}
			CAR_TEAM_ID = intent.getStringExtra("teamid");
			Carteamdata = (CarTeamIdBeen) intent.getSerializableExtra("carteamidbeen");
			L.v("获得车队的ID", "" + CAR_TEAM_ID);
		}else {
			CAR_TEAM_ID=savedInstanceState.getString("teamid");
			Carteamdata=(CarTeamIdBeen) savedInstanceState.getSerializable("Carteamdata");
		}
		Ini();
		head_title = (TextView) findViewById(R.id.head_title);
		head_title.setText("车队成员");
		head_left = (ImageView) findViewById(R.id.head_left);
		head_left.setVisibility(0);
		head_left.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View arg0) {
				// TODO Auto-generated method stub
				finish();
			}
		});

	}

	private void Ini() {
		app = (AppContent) getApplication();
		listView = (ListView) findViewById(R.id.carteammember);
		head_right_img=(ImageView) findViewById(R.id.head_right_img);
		head_right_img.setVisibility(View.VISIBLE);
		Adddriver = (Button) findViewById(R.id.Adddriver);
		listdata = new ArrayList<Map<String, Object>>();
		ImageView imageView=(ImageView) findViewById(R.id.head_right_img);
		imageView.setImageResource(R.drawable.delet_carteam);
		//TextView head_right_text=(TextView) findViewById(R.id.head_right_text);
		//imageView.setText("解散车队");
		//head_right_text.setTextColor(Color.WHITE);
		imageView.setVisibility(View.VISIBLE);
		imageView.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View arg0) {
				// TODO Auto-generated method stub
				InPutDialog inPutDialog=new InPutDialog(MyCarTeamMember.this, "请注意", "您是否要删除自己的车队",null,null);
				inPutDialog.setGetUserIntent(new getUserIntent() {

					@Override
					public void getUserIntentInfo() {
						// TODO Auto-generated method stub
						DeletCarTeam();
					}
				});
				inPutDialog.getCheckDialog(false);
				
			}
		});
		one_driver_order = new ArrayList<OrderTotal>();
		car_team_driver = new ArrayList<CarTeamIdBeen>();
		GetCarTeamRember(CAR_TEAM_ID);

		Adddriver.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View arg0) {
				// TODO Auto-generated method stub
				Intent in = new Intent(MyCarTeamMember.this,
						CarTeamAddDriver.class);
			//L.v("CAR_TEAM_ID", ""+CAR_TEAM_ID);
				in.putExtra("CarTeamId", CAR_TEAM_ID);
				startActivity(in);
			}
		});
		
	}

	private void GetCarTeamRember(String teamid) {
		HashMap<String, String> data = new HashMap<String, String>();
		data.put("start", "0");
		data.put("end", "0");
		data.put("motorcadeid", teamid);
		car_team_driver.clear();
		HttpJsonTool httpJsonTool = new HttpJsonTool(this);
		httpJsonTool.SetOnLister(new Handler() {
			@Override
			public void handleMessage(Message msg) {
				// TODO Auto-generated method stub
				super.handleMessage(msg);
				if (msg.what==1) {
					//car_team_driver.clear();
					List<CarTeamIdBeen> carMembers = (List<CarTeamIdBeen>) msg.obj;
					for (int i = 0; i < carMembers.size(); i++) {
						car_team_driver.add(carMembers.get(i));
						ListDataController();
					}
				}else {
				//	Toast.makeText(MyCarTeamMember.this, "本车队没有成员", Toast.LENGTH_LONG).show();
					if (flag==1) {
					//	car_team_driver.clear();
						ListDataController();
					}
					
				}
			
			}
		});
		Type type = new TypeToken<List<CarTeamIdBeen>>() {
		}.getType();
		httpJsonTool.StartWork(data,
				//"http://192.168.1.111:8080/cqtczh/moduls/fleets/memberlist.do",
				GagApi.GET_CAR_TEAM_REMBER,
				type, null);
	}

	private void ListDataController() {
		if (flag == 1) {
			
			L.v("数据长度", car_team_driver.size() + "");
			// listdata=getData(car_team_driver);
			if (adapter != null) {
				L.v("更新", "ListView");
				adapter.notifyDataSetChanged();
			}

			// listView.invalidateViews();
		}
		if (flag == 0) {
			L.v("flag==0", flag + "");
			ListViewIni();
		}
	}

	private void ListViewIni() {

		if (car_team_driver == null) {
			L.v("car_team_driver", "是空");
			return;
		}

		if (car_team_driver != null && car_team_driver.size() > 0) {
			adapter = new CarTeamMemberListAdapter(MyCarTeamMember.this,
					car_team_driver, R.layout.activity_my_team_list_iteam_new,1);
			adapter.setCarTeamType(false);
			listView.setAdapter(adapter);

			listView.setOnItemLongClickListener(new OnItemLongClickListener() {

				@Override
				public boolean onItemLongClick(AdapterView<?> arg0, View arg1,
						final int arg2, long arg3) {
					// TODO Auto-generated method stub

					LayoutInflater inflater = LayoutInflater
							.from(MyCarTeamMember.this);
					LinearLayout layout = (LinearLayout) inflater.inflate(
							R.layout.alert_dialog, null);
					final Dialog isExit = new AlertDialog.Builder(
							MyCarTeamMember.this).create();
					isExit.show();
					isExit.setContentView(layout);
					// 设置对话框标题
					TextView title = (TextView) layout.findViewById(R.id.title);
					TextView message = (TextView) layout
							.findViewById(R.id.message);
					title.setText("消息提醒");
					// 设置对话框消息
					message.setText("您是否删除改司机");
					Button check = (Button) layout.findViewById(R.id.check);
					Button giveup = (Button) layout.findViewById(R.id.giveup);
					check.setText("删除");
					giveup.setText("取消");
					check.setOnClickListener(new OnClickListener() {

						@Override
						public void onClick(View arg0) {
							// TODO Auto-generated method stub
							removeCarTeamDriver(car_team_driver.get(arg2)
									.getCarownerid() + "");
							isExit.dismiss();
						}
					});
					giveup.setOnClickListener(new OnClickListener() {

						@Override
						public void onClick(View arg0) {
							// TODO Auto-generated method stub
							isExit.dismiss();
						}
					});
					return false;
				}
			});

		} else if (car_team_driver.size() == 0) {
		
			L.v("本车队没有成员", "本车队没有成员");
		}
	}
	
	private void DeletCarTeam(){

		showLoadingDialog("操作中...");
		HttpJsonTool httpJsonTool=new HttpJsonTool(this);
		httpJsonTool.SetOnLister(new Handler(){
			@Override
			public void handleMessage(Message msg) {
				// TODO Auto-generated method stub
				super.handleMessage(msg);
				dismissLoadingDialog();
				if (msg.what==1) {
					Toast.makeText(MyCarTeamMember.this, (String) msg.obj, Toast.LENGTH_LONG).show();
					MyCarTeamMember.this.finish();
				}
			}
		});
		HashMap< String, String> data=new HashMap<String, String>();
		data.put("carownerid", Carteamdata.getCarownerid()+"");
		httpJsonTool.StartWork(data, GagApi.DELET_CAR_TEAM,null, null);
	}
	
	
//{"carteamId":"2","carownerid":"2"}
	private void removeCarTeamDriver(String driverID) {
		Map<String, Object> dataMap = new HashMap<String, Object>();
		//dataMap.put("carteamId", CAR_TEAM_ID);
		dataMap.put("flag", "1");
		dataMap.put("carownerid", driverID);
		HttpJsonTool httpJsonTool = new HttpJsonTool(this);
		httpJsonTool.SetOnLister(new Handler() {
			@Override
			public void handleMessage(Message msg) {
				// TODO Auto-generated method stub
				super.handleMessage(msg);
				if (msg.what == 1) {
					flag = 1;
					GetCarTeamRember(CAR_TEAM_ID);
				}
			}
		});
		httpJsonTool
				.StartWork(
						dataMap,
						//"http://192.168.1.111:8080/cqtczh/moduls/fleets/deletememberAPP.do",
						GagApi.REMOVE_CAR_TEAM_DRIVER,
						null, null);
	}

	@Override
	protected void onRestart() {
		// TODO Auto-generated method stub
		super.onRestart();
		GetCarTeamRember(CAR_TEAM_ID);
	}
	
//	@Override
//	protected void onPause() {
//		// TODO Auto-generated method stub
//		super.onPause();
//		GetCarTeamRember(CAR_TEAM_ID);
//	}
//	
	
	@Override
	protected void onActivityResult(int requestCode, int resultCode, Intent data) {
		// TODO Auto-generated method stub
		super.onActivityResult(requestCode, resultCode, data);
		if (requestCode == 100 && resultCode == 1) {
			flag = 1;
			L.v("flag=1", flag + "");
			// car_team_driver.clear();
			GetCarTeamRember(CAR_TEAM_ID);
			// adapter.notifyDataSetChanged();
			L.v("onActivityResult", "要去更新");
		}
	}

	@Override
	protected void onSaveInstanceState(Bundle outState) {
		// TODO Auto-generated method stub
		super.onSaveInstanceState(outState);
		outState.putSerializable("Carteamdata", Carteamdata);
		outState.putString("CAR_TEAM_ID", CAR_TEAM_ID);
	}

}
